package Servelts;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import daoFactory.DaoFactory;
import daoImpls.RoleDAOImpl;
import daoImpls.UserDAOImpl;
import vo.Category;
import vo.Page;
import vo.PetObject;
import vo.Role;
import vo.User;

@WebServlet("/listUser")
public class UserListServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String status = request.getParameter("status");
		String url = "error.jsp";
		User user;
		//查阅所有
		if(status.equals("selectall")){
			List<User> users = DaoFactory.getUserFactory().list();
			request.getSession().setAttribute("users", users);
			request.getSession().setAttribute("size", users.size());
			url = "listUser.jsp";
		}
		//分页查询
		if(status.equals("selectpage")){
			//获取当前页数，首次进入时start为0，点击超链接时获取start页数
	       int start=request.getParameter("start")==null?0:Integer.parseInt(request.getParameter("start"));
	       //通过page实现类获得总页数，这里的3为每页数目
	       int pageSize = 5;
	       int totalPage = DaoFactory.getPageFactory().getUserTotal(pageSize); 
	       int prePage = start-1;
	       if(prePage<0) prePage = 0;
	       int nextPage = start+1;
	       if(nextPage+1>totalPage) nextPage = totalPage-1;
	       
	       request.setAttribute("totalPage", totalPage);
	       request.setAttribute("prePage", prePage); 
	       request.setAttribute("nextPage", nextPage);
	       
	       List<User> users = DaoFactory.getUserFactory().list(start*pageSize,pageSize);
	       request.setAttribute("users", users);
	       request.setAttribute("size", users.size());
	       int totalsize = DaoFactory.getUserFactory().list().size();
	       request.setAttribute("totalsize", totalsize);
	       request.setAttribute("star", start);
	       
	       url = "listUser.jsp";
	       //url = "File.jsp";
		}
		
		//模糊查询
		if(status.equals("selectlike")){
			String s = request.getParameter("key");
			List<User> list = DaoFactory.getUserFactory().list(s);
			request.setAttribute("users", list);
			url = "listUser.jsp";
		}
		//模糊查询
		if(status.equals("selectlike2")){
			String s = request.getParameter("key");
			List<User> list = DaoFactory.getUserFactory().list(s);
			request.setAttribute("users", list);
			url = "rechargeEnter.jsp";
		}
		//删除
		if(status.equals("delect")){
			String id = request.getParameter("uid");
			DaoFactory.getUserFactory().delete(id);
			List<User> users = DaoFactory.getUserFactory().list();
			request.getSession().setAttribute("users", users);
			url = "listUser?status=selectpage";
		}
		if(status.equals("deletequery")){
			String ids = request.getParameter("ids");
			DaoFactory.getUserFactory().deleteQuery(ids);
			url = "listUser?status=selectpage";
		}
		//用户详情跳转
		if(status.equals("userinfo")){
			String id = request.getParameter("uid");
			user = DaoFactory.getUserFactory().get(id);
			request.setAttribute("user", user);
			url = "UserInfo.jsp";
			
		}
		//更新
		if(status.equals("update")){
			user = DaoFactory.getUserFactory().get(request.getParameter("uid"));
			user.setRole(new Role(request.getParameter("role")));
			user.setPwd(request.getParameter("pwd"));
			user.setSex(request.getParameter("sex"));
			user.setEmail(request.getParameter("email"));
			user.setRelName(request.getParameter("relname"));
			user.setAddress(request.getParameter("address"));
			user.setTelephone(request.getParameter("utel"));
			user.setBalance(Double.parseDouble(request.getParameter("balance")));
			DaoFactory.getUserFactory().update(user);
			url = "listUser?status=selectpage";
		}
		//权限设置
		if(status.equals("setrole")){
			String id = request.getParameter("uid");
			user = DaoFactory.getUserFactory().get(id);
			if(user.getRole().getRid().equals("2"))
				user.getRole().setRid("1");
			else 
				user.getRole().setRid("2");
			DaoFactory.getUserFactory().update(user);
			url = "listUser?status=selectpage";
		}
		//用户添加跳转
		if(status.equals("addjsp")){
			url = "Useradd.jsp";
		}
		//用户添加
		if(status.equals("add")){
			user = new User();
			user.setUname(request.getParameter("username"));
			String pwd = request.getParameter("pwd");
			if(pwd.equals(request.getParameter("pwd_sec"))){
				user.setPwd(pwd);
			}else
				response.getWriter().println("<script type='text/javascript'>  alert('密码不相同！！'); location.href='Login_Register.jsp'; </script>");
			Role role = new Role();
			role.setRid(request.getParameter("role"));
			user.setRole(role);
			user.setSex(request.getParameter("sex"));
			System.out.println(user);
			DaoFactory.getUserFactory().add(user);
			url = "listUser?status=selectpage";
		}
		
		//用户个人信息修改
		if(status.equals("myinfoupdate")){
			user = DaoFactory.getUserFactory().get(request.getParameter("uid"));
			user.setSex(request.getParameter("sex"));
			user.setEmail(request.getParameter("email"));
			user.setRelName(request.getParameter("relname"));
			user.setAddress(request.getParameter("address"));
			user.setTelephone(request.getParameter("utel"));
			DaoFactory.getUserFactory().update(user);
			url = "jump?status=myinfo";
		}
		
		//充值入口
		if(status.equals("addmoney")){
			user = DaoFactory.getUserFactory().get(request.getParameter("uid"));
			request.setAttribute("user", user);
			url = "addMoney.jsp";
			
		}
		//充值
		if(status.equals("recharge")){
			double money = Double.parseDouble(request.getParameter("balance"));
			user = DaoFactory.getUserFactory().get(request.getParameter("uid"));
			user.addBalance(money);
			DaoFactory.getUserFactory().update(user);
			url = "jump?status=recharge";
		}
		//充值
		if(status.equals("userrecharge")){
			double money = Double.parseDouble(request.getParameter("balance"));
			user = DaoFactory.getUserFactory().get(request.getParameter("uid"));
			user.addBalance(money);
			DaoFactory.getUserFactory().update(user);
			url = "jump?status=myinfo";
		}
		//密码重置
		if(status.equals("pwd")){
			String pwd = request.getParameter("pwd");
			String uid = request.getParameter("uid");
			String uname = request.getParameter("uname");
			user = DaoFactory.getUserFactory().get(uname);
			if(user.getUid().equals(uid)){
				user.setPwd(pwd);
				DaoFactory.getUserFactory().update(user);
				url = "Login?method=logined";
			}
		}
		
		request.getRequestDispatcher(url).forward(request, response);
	}
}
